package cn.zcscloud.mall.controller;

import cn.zcscloud.mall.common.R;
import cn.zcscloud.mall.template.OssTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: AliyunController
 * Description:
 * date: 2022/4/1 16:02
 *
 * @author zjf95
 * @since JDK 1.8
 */
@Slf4j
@RestController
@RequestMapping("/aliyun")
public class AliyunController {

    @Autowired
    private OssTemplate ossTemplate;

    @PostMapping("/oss/upload")
    public R policy(MultipartFile photoFile, HttpServletResponse response) {

        log.info(photoFile.getOriginalFilename());
        if (photoFile == null) {
            return R.error("数据错误");
        }
        String photoUrl = null;
        try {
            photoUrl = ossTemplate.upload(photoFile.getOriginalFilename(), photoFile.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (photoUrl == null) {
            return R.error("你死了这条心吧,你永远也得不到通往我心里的url");
        }
        Map<String, Object> map = new HashMap<>();

        map.put("url",photoUrl);

        return R.success("操作成功", map);

        ////获取文件的原始名称
        //String originalFilename = photoFile.getOriginalFilename();
        //
        ////获取原始名称的后缀
        //String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        //
        ////通过UUID来创建新的名称
        //String fileName = UUID.randomUUID() + suffix;
        //
        //File dir = new File("D:\\imgs");
        //if (!dir.exists()) {
        //    dir.mkdirs();
        //}
        ////将文件file转存到指定的文件夹中
        //try {
        //    photoFile.transferTo(new File("D:\\imgs" + fileName));
        //} catch (IOException e) {
        //    e.printStackTrace();
        //}
        //
        //try {
        //    //输入流读取硬盘中的文件
        //    FileInputStream fileInputStream = new FileInputStream(new File("D:\\imgs" + fileName));
        //
        //    //输出流将文件输出到浏览器
        //    ServletOutputStream outputStream = response.getOutputStream();
        //
        //    response.setContentType("image/jpeg");
        //    //循环读写
        //    int len = 0;
        //    byte[] bytes = new byte[1024];
        //
        //    while ((len = fileInputStream.read(bytes)) != -1) {
        //        outputStream.write(bytes, 0, len);
        //        outputStream.flush();
        //    }
        //
        //    //关流
        //    outputStream.close();
        //    fileInputStream.close();
        //} catch (Exception e) {
        //    e.printStackTrace();
        //}
        //
        //return R.success(null,null);

    }

}
